using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Drawing;


namespace Athena.Objects
{
    internal class PointFConverter : ExpandableObjectConverter
    {
        public override bool CanConvertFrom(
        ITypeDescriptorContext context, Type t)
        {

            if (t == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, t);
        }

        public override object ConvertFrom(
        ITypeDescriptorContext context,
        System.Globalization.CultureInfo info,
        object value)
        {

            if (value is string)
            {
                try
                {
                    string s = (string)value;
                    // parse the format "xPos, yPos"
                    //
                    int comma = s.IndexOf(',');
                    if (comma != -1)
                    {
                        string sXPos = s.Substring(0, comma);
                        string sYPos = s.Substring(comma + 1);

                        float xPos = Convert.ToSingle(sXPos);
                        float yPos = Convert.ToSingle(sYPos);

                        return new PointF(xPos, yPos);
                    }
                }
                catch { }
                // if we got this far, complain that we
                // couldn't parse the string
                //
                throw new ArgumentException(
                "Can not convert '" + (string)value +
                "' to type PointF");

            }
            return base.ConvertFrom(context, info, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context,
        System.Globalization.CultureInfo culture,
        object value,
        Type destType)
        {
            if (destType == typeof(string) && value is PointF)
            {
                PointF point = (PointF)value;
                return String.Format("{0}, {1}", point.X, point.Y);
            }
            return base.ConvertTo(context, culture, value, destType);
        }
    }
}
